#include "agxv_private.h"

static VkResult
queue_submit_cmd_buffer(struct agxv_queue *queue,
                        struct agxv_cmd_buffer *cmd_buffer,
                        const VkSubmitInfo *pSubmit)
{
    list_for_each_entry_safe(struct agx_job, next, &cmd_buffer->render_job, list)
        agx_submit_cmdbuf(&cmd_buffer->device->pdevice->dev, next->cmdbuf.handle, next->memmap.handle, cmd_buffer->id);

    return VK_SUCCESS;
}

static VkResult
queue_submit_cmd_buffer_batch(struct agxv_queue *queue,
                              const VkSubmitInfo *pSubmit,
                              struct agxv_queue_submit_wait_info **wait_info)
{
    uint32_t i;

    struct agxv_cmd_buffer *cmd_buffer;

    for (i = 0; i < pSubmit->commandBufferCount; i++) {
        cmd_buffer = agxv_cmd_buffer_from_handle(pSubmit->pCommandBuffers[i]);
        queue_submit_cmd_buffer(queue, cmd_buffer, pSubmit);
    }

    return VK_SUCCESS;
}

VKAPI_ATTR VkResult VKAPI_CALL
agxv_QueueSubmit(VkQueue _queue,
                 uint32_t submitCount,
                 const VkSubmitInfo *pSubmits,
                 VkFence fence)
{
    AGXV_FROM_HANDLE(agxv_queue, queue, _queue);

    uint32_t i;
    VkResult result = VK_SUCCESS;

    struct agxv_queue_submit_wait_info *wait_info;

    for (i = 0; i < submitCount; i++) {
        result = queue_submit_cmd_buffer_batch(queue, &pSubmits[i], &wait_info);
        if (result != VK_SUCCESS && result != VK_NOT_READY)
            goto done;
    }

done:

    return 0;
}